#pragma once

#include "baseType.h"
#include "basefunc.h"

#include <map>
using namespace std;

///////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////

template	<TYPENAME K, TYPENAME D>
class CIndex
{
public:
	CIndex() {}
	virtual ~CIndex() {}
public:
	bool		add			(K key, D data)					{ m_map[key] = data; return true; }
	bool		del			(K key)							{ m_map.erase(key); return true; }
	bool		is_in		(K key)	const					{ if(m_map.find(key) !=m_map.end()) return true; return false; }
	const D&	at			(K key)	const					{ return operator[](key); }
	D&			operator[]	(K key);
	const D&	operator[]	(K key) const;
	void		clear		(void) { m_map.clear(); }
	typedef	map<K, D> MAPINDEX;
	typedef	MAPINDEX::iterator	Iterator;
	typedef	MAPINDEX::const_iterator	ConstIterator;
	const K&	key			(Iterator iter)	const			{ return iter->first; }
	const D&	data		(Iterator iter)	const			{ return iter->second; }
	Iterator	begin		(void)							{ return m_map.begin(); }
	Iterator	end			(void)							{ return m_map.end(); }
	unsigned long	size	(void)							{ return m_map.size();}
public: // dump
	CStr		dump		(void)	const					{ CStr str="{ ("; 
																MAPINDEX::const_iterator iter=m_map.begin(); 
																for(; iter!=m_map.end(); iter++){ 
																	if(iter!=m_map.begin()) str+="), (";
																		str+=::dump_string(iter->first);
																		str+=",";
																		str+=::dump_string(iter->second); } 
																	return str+=") }"; }
protected:
	MAPINDEX	m_map;
};

template	<TYPENAME K, TYPENAME D>
D&	CIndex<K, D>::operator[](K key)
{
	Iterator iter	= m_map.find(key);
	ASSERT(iter !=m_map.end());
	return iter->second;
}

template	<TYPENAME K, TYPENAME D>
const D&	CIndex<K, D>::operator[](K key) const
{
	ConstIterator iter	= m_map.find(key);
	ASSERT(iter !=m_map.end());
	return iter->second;
}

